#!/bin/bash
#
# Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
#
# SPDX-License-Identifier: LGPL-2.1-only

TEST_DESC="Metadata env - User space tracing"

CURDIR=$(dirname "$0")/
TESTDIR=$CURDIR/../../..
TESTAPP_PATH="$TESTDIR/utils/testapp"
TESTAPP_NAME="gen-ust-events"
TESTAPP_BIN="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME"
EVENT_NAME="tp:tptest"

TRACE_PATH=$(mktemp -d -t tmp.test_metadata_env_ust_trace_path.XXXXXX)

NUM_TESTS=37

NR_ITER=10
NR_USEC_WAIT=0

source "$TESTDIR/utils/utils.sh"

# Fetch utils functions common to ust and kernel tests.
source "$CURDIR/utils.sh"

if [ ! -x "$TESTAPP_BIN" ]; then
	BAIL_OUT "No UST events binary detected."
fi

function test_per_uid ()
{
	local metadata_path
	local metadata
	local metadata_env

	local expected_path="${TRACE_PATH}/lttng-traces"
	local session_name="per-uid"

	metadata=$(mktemp -t tmp.test_per_uid_metadata.XXXXXX)
	metadata_env=$(mktemp -t tmp.test_per_uid_metadata_env.XXXXXX)

	diag "Test UST metadata env field in per-uid mode"
	create_lttng_session_ok "$session_name"
	enable_ust_lttng_event_ok "$session_name" $EVENT_NAME
	start_lttng_tracing_ok "$session_name"

	$TESTAPP_BIN -i $NR_ITER

	stop_lttng_tracing_ok "$session_name"
	destroy_lttng_session_ok "$session_name"

	# bt1 accepts only a directory while bt2 accepts either the metadata
	# file directly or a directory with an immediate metadata file.
	# Settle for the common denominator.
	metadata_path=$(find "${expected_path}/${session_name}"* -name "metadata")
	metadata_path=$(dirname "$metadata_path")

	_run_babeltrace_cmd --output-format=ctf-metadata "${metadata_path}" > "$metadata"

	# Extract "env" scope
	awk '/env {/,/};/' < "$metadata" > "$metadata_env"

	# Construct the expected path from the env metadata and use it to
	# validate that all information make sense. This information is present
	# to allow trace viewer to recreate the same directory hierarchy.

	# Trace name
	local trace_name
	trace_name=$(get_env_value "$metadata_env" trace_name)
	ok $? "Extracting trace_name from env metadata: \`$trace_name\`"
	expected_path="${expected_path}/${trace_name}"

	# Session creation time
	local trace_creation_datetime
	trace_creation_datetime=$(get_env_value "$metadata_env" trace_creation_datetime)
	ok $? "Extracting trace_creation_datetime from env metadata: \`$trace_creation_datetime\`"
	trace_creation_datetime=$(iso8601_to_lttng_dir_datetime "$trace_creation_datetime")
	expected_path="${expected_path}-${trace_creation_datetime}"

	# Domain
	local domain
	domain=$(get_env_value "$metadata_env" domain)
	ok $? "Extracting domain from env metadata: \`$domain\`"
	expected_path="${expected_path}/${domain}"

	# Buffering scheme
	local tracer_buffering_scheme
	tracer_buffering_scheme=$(get_env_value "$metadata_env" tracer_buffering_scheme)
	ok $? "Extracting tracer_buffering_scheme from env metadata: \`$tracer_buffering_scheme\`"
	expected_path="${expected_path}/${tracer_buffering_scheme}"

	# tracer_buffering_id
	local tracer_buffering_id
	tracer_buffering_id=$(get_env_value "$metadata_env" tracer_buffering_id)
	ok $? "Extracting tracer_buffering_id from env metadata: \`$tracer_buffering_id\`"
	expected_path="${expected_path}/${tracer_buffering_id}"

	# Check that the uid is the one we expect from the current user
	is "$tracer_buffering_id" "$(id -u)" "tracer_buffering_id match current user uid: \`$tracer_buffering_id\`"

	# Architecture_bit_width
	local architecture_bit_width
	architecture_bit_width=$(get_env_value "$metadata_env" architecture_bit_width)
	ok $? "Extracting architecture_bit_width from env metadata: \`$architecture_bit_width\`"
	expected_path="${expected_path}/${architecture_bit_width}-bit"

	# Append "metadata" and test that we find the file.
	expected_path="${expected_path}/metadata"

	test -f "$expected_path"
	ok $? "Reconstructed path from metadata is an existing file: \`$expected_path\`"

	# Hostname
	# The hostname is not part of the lttng hierarchy still we can test for
	# its validity here.
	local hostname
	hostname=$(get_env_value "$metadata_env" hostname)
	ok $? "Extracting hostname from env metadata: \`$hostname\`"
	is "$hostname" "$(hostname)" "Extracted hostname matches current hostname"

	rm -f "$metadata"
	rm -f "$metadata_env"
}

function test_per_pid ()
{
	local metadata_path
	local metadata
	local metadata_env
	local testapp_pid

	local expected_path="${TRACE_PATH}/lttng-traces"
	local session_name="per-pid"

	metadata=$(mktemp -t tmp.test_per_pid_metadata.XXXXXX)
	metadata_env=$(mktemp -t tmp.test_per_pid_metadata_env.XXXXXX)

	diag "Test UST metadata env field in per-pid mode"
	create_lttng_session_ok "$session_name"
	enable_ust_lttng_channel_ok "$session_name" channel "--buffers-pid"
	enable_ust_lttng_event_ok "$session_name" $EVENT_NAME channel
	start_lttng_tracing_ok "$session_name"

	$TESTAPP_BIN -i $NR_ITER -w $NR_USEC_WAIT &
	testapp_pid=$!
	wait $testapp_pid

	stop_lttng_tracing_ok "$session_name"
	destroy_lttng_session_ok "$session_name"

	# bt1 accepts only a directory while bt2 accepts either the metadata
	# file directly or a directory with an immediate metadata file.
	# Settle for the common denominator.
	metadata_path=$(find "${expected_path}/${session_name}"* -name "metadata")
	metadata_path=$(dirname "$metadata_path")

	_run_babeltrace_cmd --output-format=ctf-metadata "${metadata_path}" 1> "$metadata"

	# Extract "env" scope
	awk '/env {/,/};/' < "$metadata" > "$metadata_env"

	# Construct the expected path from the env metadata and use it to
	# validate that all information make sense. This information is present
	# to allow trace viewer to recreate the same directory hierarchy.

	# Trace name
	local trace_name
	trace_name=$(get_env_value "$metadata_env" trace_name)
	ok $? "Extracting trace_name from env metadata: \`$trace_name\`"
	expected_path="${expected_path}/${trace_name}"

	# Session creation time
	local trace_creation_datetime
	trace_creation_datetime=$(get_env_value "$metadata_env" trace_creation_datetime)
	ok $? "Extracting trace_creation_datetime from env metadata: \`$trace_creation_datetime\`"
	trace_creation_datetime=$(iso8601_to_lttng_dir_datetime "$trace_creation_datetime")
	expected_path="${expected_path}-${trace_creation_datetime}"

	# Domain
	local domain
	domain=$(get_env_value "$metadata_env" domain)
	ok $? "Extracting domain from env metadata: \`$domain\`"
	expected_path="${expected_path}/${domain}"

	# Buffering scheme
	local tracer_buffering_scheme
	tracer_buffering_scheme=$(get_env_value "$metadata_env" tracer_buffering_scheme)
	ok $? "Extracting tracer_buffering_scheme from env metadata: \`$tracer_buffering_scheme\`"
	expected_path="${expected_path}/${tracer_buffering_scheme}"

	# Procname
	local procname
	procname=$(get_env_value "$metadata_env" procname)
	ok $? "Extracting procname from env metadata: \`$procname\`"
	expected_path="${expected_path}/${procname}"

	# vpid and tracer_buffering_id should be the same here.
	local vpid
	vpid=$(get_env_value "$metadata_env" "vpid")
	ok $? "Extracting vpid from env metadata: \`$vpid\`"

	local tracer_buffering_id
	tracer_buffering_id=$(get_env_value "$metadata_env" tracer_buffering_id)
	ok $? "Extracting tracer_buffering_id from env metadata: \`$tracer_buffering_id\`"
	expected_path="${expected_path}-${tracer_buffering_id}"

	is "$vpid" "$tracer_buffering_id" "vpid and tracer_buffering_id are identical"
	is "$testapp_pid" "$vpid" "vpid and known testapp pid are identical"
	is "$testapp_pid" "$tracer_buffering_id" "tracer_buffering_id and known testapp pid are identical"

	# vpid_datetime
	local vpid_datetime
	vpid_datetime=$(get_env_value "$metadata_env" vpid_datetime)
	ok $? "Extracting vpid_datetime from env metadata: \`$vpid_datetime\`"
	vpid_datetime=$(iso8601_to_lttng_dir_datetime "$vpid_datetime")
	expected_path="${expected_path}-${vpid_datetime}"

	# Append "metadata" and test that we find the file.
	expected_path="${expected_path}/metadata"

	test -f "$expected_path"
	ok $? "Reconstructed path from metadata is an existing file: \`$expected_path\`"

	# Hostname
	# The hostname is not part of the lttng hierarchy still we can test for
	# its validity here.
	local hostname
	hostname=$(get_env_value "$metadata_env" hostname)
	ok $? "Extracting hostname from env metadata: \`$hostname\`"
	is "$hostname" "$(hostname)" "Extracted hostname matches current hostname"

	rm -f "$metadata"
	rm -f "$metadata_env"
}

plan_tests $NUM_TESTS

print_test_banner "$TEST_DESC"

bail_out_if_no_babeltrace

# Use LTTNG_HOME since we want the complete "default" lttng directory hierarchy
# with "<session_name>-<datetime>/...".
export LTTNG_HOME="$TRACE_PATH"

start_lttng_sessiond

tests=( test_per_uid test_per_pid )

for fct_test in "${tests[@]}";
do
	${fct_test}
done

stop_lttng_sessiond
unset LTTNG_HOME

rm -rf "$TRACE_PATH"
